package com.example.nbshopping.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.nbshopping.R;
import com.example.nbshopping.utils.BaseActivity;
import com.example.nbshopping.utils.SaveUserUtils;
import com.example.nbshopping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class ChangePwdActivity extends BaseActivity {
    private UserBean.DataBean userInfo;
    EditText pwdEt,npwdEt,rnpwdEt;
    Button ensureBtn,backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        userInfo= SaveUserUtils.getUserInfo(this);
        initView();
        setListener();
    }

    private void setListener() {
        ensureBtn.setOnClickListener(listener);
        backBtn.setOnClickListener(listener);
    }

    private void initView() {
        pwdEt = findViewById(R.id.frag_cpwd_et1);
        npwdEt = findViewById(R.id.frag_cpwd_et2);
        rnpwdEt = findViewById(R.id.frag_cpwd_et3);
        ensureBtn = findViewById(R.id.frag_cpwd_btn);
        backBtn=findViewById(R.id.frag_cpwd_btn2);
    }
    View.OnClickListener listener= new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            int id=v.getId();
            String pwd = pwdEt.getText().toString();
            String npwd = npwdEt.getText().toString();
            String rnpwd = rnpwdEt.getText().toString();
            switch (id) {
                case R.id.frag_cpwd_btn:
                    if(judgeInput(pwd,npwd,rnpwd)) {
                        Map<String, String> map = new HashMap<>();
                        map.put("userId", userInfo.getId() + "");
                        map.put("oldPwd", userInfo.getPassword());
                        map.put("newPwd",npwd );
                        postNetwork(URLUtils.updatePwd_url, map);
                    }
                    break;
                case R.id.frag_cpwd_btn2:
                    finish();
                    break;
            }
        }
    };
    public void onSuccess(String result) {
        super.onSuccess(result);
        UserBean jsonbean = new Gson().fromJson(result, UserBean.class);
        switch (jsonbean.getCode()) {
            case 200:
                SaveUserUtils.saveUserToFile(this,userInfo);
                userInfo = SaveUserUtils.getUserInfo(this);
                Toast.makeText(ChangePwdActivity.this,"修改密码成功！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(ChangePwdActivity.this, LoginRegActivity.class);
                startActivity(intent);
                break;
            case 201:
                Toast.makeText(ChangePwdActivity.this,"原始密码错误！",Toast.LENGTH_SHORT).show();
            default:
                Toast.makeText(ChangePwdActivity.this,"参数错误！",Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private boolean judgeInput(String oldpwd,String newpwd,String ensurepwd){
        //判断输入内容为空，则不能登录
        if (TextUtils.isEmpty(oldpwd)|| TextUtils.isEmpty(newpwd)||TextUtils.isEmpty(ensurepwd)) {
            Toast.makeText(getBaseContext(),"输入内容不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.equals(oldpwd,newpwd)) {
            Toast.makeText(getBaseContext(),"新旧密码相同，请重新输入！",Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!TextUtils.equals(newpwd,ensurepwd)) {
            Toast.makeText(getBaseContext(),"请输入相同的新密码！",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
